<template>
	<uni-search-bar @confirm="toSearch" placeholder="请输入搜索的公交线路" v-model="searchKey" />
	<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
		<swiper-item v-for="(item,index) in lunboList" :key="index">
			<view class="swiper-item">
				<img :src="item.url" style="width: 100%;" />
			</view>
		</swiper-item>
	</swiper>

	<view class="weather" style="height: 100rpx;border: 1px solid #ccc;margin: 5px;
	 border-radius: 5px;display: flex;justify-content: space-between; padding: 10rpx;align-items: center;">
		<view class="icon" style="width: 100rpx;height: 100rpx; ">
			<image style="width: 100%;height: 100%;" :src="'/static/weather/icons/'+ weatherNow.icon +'.svg'" mode="">
			</image>
		</view>
		<view class="text" style="font-size: 50rpx;margin-left: 10px;">
			{{weatherNow.temp}}°C
		</view>
		<view class="text" style="font-size: 34rpx;margin-left: 10rpx;">
			{{weatherNow.text}}
		</view>
		<view class="text" style=" margin-left: 10rpx;">
			{{weatherNow.windDir}} {{weatherNow.windScale}}级
		</view>
		<view>
			<view class="text" style="margin-left: 10rpx;">
				体感温度： {{weatherNow.feelsLike }}°C
			</view>
			<view class="text" style="margin-left: 10rpx;">
				湿度： {{weatherNow.humidity }}%
			</view>
		</view>
	</view>

	<uni-grid :show-border="false" :column="5">
		<uni-grid-item v-for="(item, index) in gridData" :key="index" @click="changeGrid(index)">
			<view class="grid-item">
				<view class="grid-item-icon">
					<uni-icons :type="item.icon" :size="50" :color="item.color" />
				</view>
				<text class="grid-item-text">{{ item.title }}</text>
			</view>
		</uni-grid-item>
	</uni-grid>

	<view class="xianvideo">
		<video src="../../static/video/xian.mp4" autoplay muted loop controls></video>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue'
	import {
		onLoad
	} from "@dcloudio/uni-app"
	import {
		CONFIG
	} from "../../utils/config"
	import {
		userStore
	} from '../../store/userStrore'
	const store = userStore()
	const searchKey = ref("")

	function toSearch() {
		if (searchKey.value === "") {
			uni.showToast({
				title: "请输入查询的公交地图线路",
				icon: "none"
			})
		} else {
			window.location.href = `https://uri.amap.com/line?name=${searchKey.value}&city=029&callnative=1`
		}
	}

	onLoad(() => {
		getLuinbo()
		getWeather()
		getCurrentPos()
	})
	let lunboList = ref([])
	async function getLuinbo() {
		let res = await uni.$get(CONFIG.leanbase + "/1.1/classes/LunBo")
		if (res.statusCode == 200) {
			lunboList.value = res.data.results
		}
	}

	let weatherNow = ref({})
	async function getWeather() {
		let res = await uni.$get(CONFIG.hefeng_now, {
			key: CONFIG.hefeng_key,
			location: CONFIG.hefeng_location
		})
		console.log("lunbo：" + res.data.code)
		if (res.data.code === "200") {
			weatherNow.value = res.data.now
		}
	}

	function getCurrentPos() {
		uni.getLocation({
			success: function(res) {
				console.log('当前位置的经度：' + res.longitude);
				console.log('当前位置的纬度：' + res.latitude);
				// 将定位保存到状态库
				store.setPos(res.longitude, res.latitude)
			}
		});

	}

	let gridData = [{
		title: '问答',
		icon: 'videocam',
		color: "red" //图标颜色
	}, {
		title: '我是文字',
		icon: 'star',
		color: "red" //图标颜色
	}, {
		title: '我是文字',
		icon: 'videocam',
		color: "red" //图标颜色
	}, {
		title: '我是文字',
		icon: 'videocam',
		color: "red" //图标颜色
	}, {
		title: '我是文字',
		icon: 'videocam',
		color: "red" //图标颜色
	}, {
		title: '我是文字',
		icon: 'videocam',
		color: "red" //图标颜色
	}, {
		title: '我是文字',
		icon: 'videocam',
		color: "red" //图标颜色
	}, {
		title: '我是文字',
		icon: 'videocam',
		color: "red" //图标颜色
	}, {
		title: '我是文字',
		icon: 'videocam',
		color: "red" //图标颜色
	}, {
		title: '我是文字',
		icon: 'videocam',
		color: "red" //图标颜色
	}]

	function changeGrid(e) {
		//点击的序号
		console.log(e)
		switch (e) {
			case 0:
				uni.switchTab({
					url: '/pages/question/question'
				})
		}
	}
</script>

<style lang="scss" scoped>
	.grid-item {
		width: 80px;
		text-align: center;

		.grid-item-icon {}

		.grid-item-text {
			font-size: 15px;
			line-height: 30px;
		}
	}

	video {
		width: 100%;
	}
</style>